package zyx20230706;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;

//继承接口:public class 子类 implements 接口，接口，，{}
//继承接口一定要重写接口中所有的抽象方法
public class DrawMouse implements MouseListener, ActionListener, MouseMotionListener {
    //引用传递(初始化属性)
    public Graphics gr; //保存传递过来的画笔对象
    //全局变量：属性
    public int x1,y1,x2,y2,x3,y3,x4,y4;
    public String name;
    //定义标记位，用来控制代码的执行过程
    public int flag=1;
    //保存当前选择的颜色
    public Color color = Color.BLACK;

    //定义Shape 数组，保存创建的图形对象
    public Shape[] shapeArr = new Shape[100];

    //操作数组的下标
    public int index = 0;
    //public int counter=0;


    public void actionPerformed(ActionEvent e){
        //获取按钮上的内容
        name = e.getActionCommand();

        //25-33代码作用：判断点击的按钮是颜色还是图形按钮
        if(name.equals("")) {
            //获取当前事件源对象
            JButton btu = (JButton) e.getSource();
            color = btu.getBackground();
            gr.setColor(color);
        }
        else{
            name = e.getActionCommand();
        }
        System.out.println("点击按钮！ name = "+name);
        if("随机数".equals(name)) {
            Random ran = new Random();
            int a[]= {0,0,0,0,0,0,0,0};
            for(int i=0;i<8;i++) {
                int num = (int) (Math.random() * 500);//ran.nextInt(700);
                a[i]=num;
            }
            x1=a[0];
            x2=a[1];
            y1=a[2];
            y2=a[3];
            x3=a[4];
            y3=a[5];
            x4=a[6];
            y4=a[7];
            for(int i=0;i<10000;i++) {
                int num1 = ran.nextInt(3);
                if(num1==0) {
                    x2=(x1+x2)/2;
                    y2=(y1+y2)/2;
                } else if(num1==1){
                    x2=(x3+x2)/2;
                    y2=(y3+y2)/2;
                }else if(num1==2){
                    x2=(x4+x2)/2;
                    y2=(y4+y2)/2;
                }
                gr.drawLine(x2, y2, x2, y2);
            }
        }
    }

    public void mouseClicked(MouseEvent e){
        System.out.println("点击");
        x3 = e.getX();
        y3 = e.getY();
        Shape shape = new Shape();

        if ("三角形".equals(name))
        {
            gr.drawLine(x1, y1, x3, y3);
            gr.drawLine(x2, y2, x3, y3);
            shape.color=color;
            shape.name = name;
            shape.x1 = x1;
            shape.y1 = y1;
            shape.x2 = x2;
            shape.y2 = y2;
            shape.x3 = x3;
            shape.y3 = y3;
            shapeArr[index++] = shape;
            flag=1;
        }
        if("多边形".equals(name)) {
            gr.drawLine (x2, y2, x3, y3);
            //判断是否双击
            if(e.getClickCount()==2){
                gr.drawLine(x3, y3, x1, y1);
            }
            x2=x3;//让当前线段的终点变成下条线段的起点
            y2=y3;
        }
    }

    public void mousePressed(MouseEvent e){
        System.out.println("按下");
        //获取当前坐标值
        //局部变量：1.在当前方法(作用域)中定义的变量 2.方法的参数
        if(flag == 1) {
            x1 = e.getX();
            y1 = e.getY();
        }
    }
    public void mouseReleased(MouseEvent e){
        System.out.println("释放");
        if(flag == 1) {
            x2 = e.getX();
            y2 = e.getY();
        }
//绝对值
        int w = Math.abs(x2-x1);
        int h = Math.abs(y2-y1);

//绘制图形
        if("直线".equals(name)) {
            Shape shape = new Shape();
            shape.x1 = x1;
            shape.y1 = y1;
            shape.x2 = x2;
            shape.y2 = y2;
            shape.name = name;
            shape.color=color;

            //保存对象到数组中
            shapeArr[index++] = shape;
            gr.drawLine(x1, y1, x2, y2);
        }

        if("矩形".equals(name)) {
// 矩形左上角的坐标 + 宽 高
            gr.drawRect(x1, y1, w, h);
            Shape shape = new Shape();
            shape.name = name;
            shape.color=color;
            shape.x1 = x1;
            shape.y1 = y1;
            shape.x2 = w;
            shape.y2 = h;
            shapeArr[index++] = shape;
        }
        if("三角形".equals(name)||"多边形".equals(name)&& flag==1 ) {
            gr.drawLine(x1, y1, x2, y2);
            flag++;
        }
// 外切矩形的左上角坐标 + 宽 高
        if("圆".equals(name)){
            Shape shape = new Shape();
            shape.x1 = Math.min (x1, x2);
            shape.y1 = Math.min (y1, y2);
            shape.x2 = Math.abs (x2 - x1);
            shape.y2 = Math.abs (y2 - y1);
            shape.name = name;
            shape.color=color;

            //保存对象到数组中
            shapeArr[index++] = shape;
            gr.drawOval (Math.min (x1, x2), Math.min (y1, y2),
                    Math.abs (x2 - x1), Math.abs (y2 - y1));
        }
    }
    public void mouseDragged(MouseEvent e) {
        if ("铅笔".equals(name)) {
            x4 = e.getX();
            y4 = e.getY();
            gr.drawLine(x1, y1, x4, y4);
            x1 = x4;
            y1 = y4;
               /* counter++;
                while (true) {
                    Shape shape = new Shape();
                    shape.x1 = x1;
                    shape.y1 = y1;
                    shape.x2 = x4;
                    shape.y2 = y4;
                    shape.name = name;
                    shape.color = color;
                    shapeArr[index++] = shape;
                    if (e.getClickCount() == 2)
                        break;
                }*/
        }
    }

    public void mouseMoved(MouseEvent e){
    }
    public void mouseEntered(MouseEvent e){
    }
    public void mouseExited(MouseEvent e){
    }
}

